﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Windows;
using System.Windows.Input;
using System.Windows.Controls;
using Microsoft.Practices.Prism.Commands;


namespace ListViewItemFloatTest
{
    public static class MouseDoubleClick
    {
        public static readonly DependencyProperty CommandProperty =
            DependencyProperty.RegisterAttached(
            "Command",
            typeof(ICommand),
            typeof(MouseDoubleClick),
            new PropertyMetadata(OnSetCommandCallBack));

        public static readonly DependencyProperty CommandParameterProperty =
                                DependencyProperty.RegisterAttached(
                                        "CommandParameter",
                                        typeof(object),
                                        typeof(MouseDoubleClick),
                                        new PropertyMetadata(OnSetCommandParameterCallBack));

        private static readonly DependencyProperty MouseDoubleClickCommandBehaviorProperty =
            DependencyProperty.RegisterAttached(
            "MouseDoubleClickCommandBehavior",
            typeof(ListViewItemDoubleClickCommandBehavior),
            typeof(MouseDoubleClick),
            null);


        #region Get and Set Methods For The Command and CommandParameter Properties

        public static void SetCommand(ListViewItem listViewItem, ICommand value)
        {
            listViewItem.SetValue(CommandProperty, value);
        }

        public static ICommand GetCommand(ListViewItem listViewItem)
        {
            return (ICommand)listViewItem.GetValue(CommandProperty);
        }

        public static void SetCommandParameter(ListViewItem listViewItem, object value)
        {
            listViewItem.SetValue(CommandParameterProperty, value);
        }

        public static object GetCommandParameter(ListViewItem listViewItem)
        {
            return (object)listViewItem.GetValue(CommandParameterProperty);
        }

      
        #endregion

        private static void OnSetCommandCallBack(DependencyObject dependencyObject,
            DependencyPropertyChangedEventArgs e)
        {
            ListViewItem listViewItem = dependencyObject as ListViewItem;
            if (listViewItem != null)
            {
                ListViewItemDoubleClickCommandBehavior behavior = GetOrCreateBehavior(listViewItem);
                behavior.Command = e.NewValue as ICommand;
            }
        }

        private static void OnSetCommandParameterCallBack(DependencyObject dependencyObject,
            DependencyPropertyChangedEventArgs e)
        {
            ListViewItem listViewItem = dependencyObject as ListViewItem;
            if (listViewItem != null)
            {
                ListViewItemDoubleClickCommandBehavior behavior = GetOrCreateBehavior(listViewItem);
                behavior.CommandParameter = e.NewValue;
            }
        }

        /// <summary>
        /// Gets or creates the behavior attached to the ListViewItem object.
        /// </summary>
        /// <param name="listViewItem">The ListViewItem object.</param>
        /// <returns>The behavior attached to the ListViewItem object.</returns>
        private static ListViewItemDoubleClickCommandBehavior GetOrCreateBehavior(
            ListViewItem listViewItem)
        {
            ListViewItemDoubleClickCommandBehavior behavior =
                listViewItem.GetValue(MouseDoubleClickCommandBehaviorProperty) as
                ListViewItemDoubleClickCommandBehavior;

            if (behavior == null)
            {
                behavior = new ListViewItemDoubleClickCommandBehavior(listViewItem);
                listViewItem.SetValue(MouseDoubleClickCommandBehaviorProperty, behavior);
            }

            return behavior;
        }
    }
}
